package com.example.fastshare.adapter;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.example.fastshare.entities.GroupEntity;

public class DBGroupAdapter extends BaseAdapter {

	private DatabaseAdapter mDatabaseAdapter;
	private Context context;
	private ArrayList<Bitmap> arrayBitMap;

	public DBGroupAdapter(Context context) {
		super();
		mDatabaseAdapter = new DatabaseAdapter(context);
	}

	public DBGroupAdapter(Context context, ArrayList<Bitmap> arrayBitMap) {
		super();
		this.context = context;
		this.arrayBitMap = arrayBitMap;
	}

	public DatabaseAdapter getmDatabaseAdapter() {
		return mDatabaseAdapter;
	}

	public void setmDatabaseAdapter(DatabaseAdapter mDatabaseAdapter) {
		this.mDatabaseAdapter = mDatabaseAdapter;
	}

	public Context getContext() {
		return context;
	}

	public void setContext(Context context) {
		this.context = context;
	}

	public ArrayList<Bitmap> getArrayBitMap() {
		return arrayBitMap;
	}

	public void setArrayBitMap(ArrayList<Bitmap> arrayBitMap) {
		this.arrayBitMap = arrayBitMap;
	}

	public long insertGroup(GroupEntity entity) {
		if (entity != null) {
			ContentValues values = new ContentValues();
			values.put(GroupEntity.ID, entity.getID());
			Log.d("Insert Group", entity.getID());
			values.put(GroupEntity.NAME, entity.getName());
			Log.d("Insert Group", entity.getName());
			values.put(GroupEntity.DESCRIPTION, entity.getDescription());
			Log.d("Insert Group", entity.getDescription());
			values.put(GroupEntity.ICON_PATH, entity.getIcon_Path());
			Log.d("Insert Group", entity.getIcon_Path());

			long result = mDatabaseAdapter.insert(GroupEntity.TABLE_NAME, null,
					values);
			Log.d("Insert Group", "result: " + result);
			return result;
		} else
			return -1;
	}

	public GroupEntity getGroupByID(String id) {
		GroupEntity result = null;

		String where = GroupEntity.ID + "='" + id + "'";
		Cursor cur = mDatabaseAdapter.query(GroupEntity.TABLE_NAME, null,
				where, null, null, null, null);

		if (cur != null && cur.getCount() > 0) {
			result = getGroupEntity(cur);
		}

		cur.close();

		return result;
	}

	public List<GroupEntity> getAllGroup() {
		List<GroupEntity> result = null;

		String orderASC = GroupEntity.ID + " ASC ";

		Cursor cur = mDatabaseAdapter.query(GroupEntity.TABLE_NAME, null, null,
				null, null, null, orderASC);

		if (cur != null && cur.getCount() > 0) {
			result = new ArrayList<GroupEntity>();
			while (!cur.isAfterLast()) {
				result.add(getGroupEntity(cur));
				cur.moveToNext();
			}
		}
		cur.close();
		return result;
	}

	public boolean isEmptyGroup() {
		boolean result = true;

		Cursor cur = mDatabaseAdapter.query(GroupEntity.TABLE_NAME, null, null,
				null, null, null, null);

		if (cur != null && cur.getCount() > 0) {
			result = false;
		}
		cur.close();
		return result;
	}

	public long deleteGroupByID(String id) {
		String where = GroupEntity.ID + "='" + id + "'";
		return mDatabaseAdapter.delete(GroupEntity.TABLE_NAME, where, null);
	}

	public long deleteAllImages() {
		return mDatabaseAdapter.delete(GroupEntity.TABLE_NAME, null, null);
	}

	private GroupEntity getGroupEntity(Cursor mCursor) {
		if (mCursor != null) {
			GroupEntity values = new GroupEntity();
			values.setID(mCursor.getString(mCursor
					.getColumnIndex(GroupEntity.ID)));
			values.setName(mCursor.getString(mCursor
					.getColumnIndex(GroupEntity.NAME)));
			values.setIcon_Path(mCursor.getString(mCursor
					.getColumnIndex(GroupEntity.ICON_PATH)));
			values.setDescription(mCursor.getString(mCursor
					.getColumnIndex(GroupEntity.DESCRIPTION)));
			return values;
		}
		return null;
	}

	@Override
	public int getCount() {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public Object getItem(int position) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public long getItemId(int position) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		// TODO Auto-generated method stub
		return null;
	}
}
